<?php
/**
 * Trendopedia
 *
 * 
 * @package    Plugin
 * 
 */

/** Zend_Controller_Plugin_Abstract */
require_once('Zend/Controller/Plugin/Abstract.php');

/** Zend_Config_ini */
require_once('Zend/Config/Ini.php');



class Router extends Zend_Controller_Plugin_Abstract
{
	public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
		$moduleDirectoryName = '../app_mvc';
		
     	$CONFIG = Zend_Registry::get('config');
 		$CACHE = Zend_Registry::get('cache');
		
		if($CONFIG->application_state == 'production') {
			if (!$ROUTE = $CACHE->load('route')) {
				$ROUTE = new Zend_Config_Ini('../config/Route.ini','production',true);
				$modules = scandir($moduleDirectoryName);
				foreach($modules as $module) {
					$routeIni = "../app_mvc/$module/config/Route.ini";
					if (file_exists($routeIni))	{
						$route_config = new Zend_Config_Ini($routeIni,'production',true);
						$ROUTE->merge($route_config);
					}
				}
				$CACHE->save($ROUTE, 'route');
			}
		} else {
			$ROUTE = new Zend_Config_Ini('../config/Route.ini','production',true);
			if(MODULE_TYPE == 'admin') {
				$routeIni = "../app_mvc/default/config/AdminRoute.ini";
				$route_config = new Zend_Config_Ini($routeIni,'production',true);
				$ROUTE->merge($route_config);
			} else{
			
				$modules = scandir($moduleDirectoryName);
				foreach($modules as $module) {
					$routeIni = "../app_mvc/$module/config/Route.ini";
					if (file_exists($routeIni))	{
						$route_config = new Zend_Config_Ini($routeIni,'production',true);
						$ROUTE->merge($route_config);
					}
				}
			}
		}
		
	
		
		$main = Zend_Controller_Front::getInstance();
		$router = $main->getRouter();
		
	
		$router->addConfig($ROUTE, 'routes');
    }
}
?>